//
//  XCUnderSingerViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/16.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCUnderSingerViewController.h"
#import "XCUnderSingerTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "XCAFNetTool.h"
#import "XCUnderSingerDataModel.h"
#import "XCUrlManger.h"
#import "MJRefresh.h"
#import "OnlineAVplay.h"
#import "XCPlayViewController.h"
@interface XCUnderSingerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSString *picUrl;

@property (nonatomic, retain) NSNumber *singer_id;

@property (nonatomic, retain) NSNumber *page;

@property (nonatomic, assign) NSInteger pageChange;

@property (nonatomic, retain) UILabel *UnderSingerNameLable;

@property (nonatomic, retain) UIImageView *UnderSingerHeadImageView;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;

@end

@implementation XCUnderSingerViewController


- (instancetype)initWithSinger_id:(NSNumber *)singer_id
{
    self = [super init];
    if (self) {
        self.singer_id = singer_id;
        
    }
    return self;
}



- (void)dealloc {
    
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_tableView release];
    [_UnderSingerHeadImageView release];
    [_UnderSingerNameLable release];
    [_picUrl release];
    [_dataSource release];
    [_singer_id release];
    [_mutableArraySingerNAme release];
    [_mutableArraySongName release];
    [_nameArray release];
    [_mutableHeadIVPicurl release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageChange = 1;
    self.navigationItem.title = @"个人秀";
    [self UnderSingerNetRequest];
    [self.tableView addFooterWithTarget:self action:@selector(UnderSingerNetRequest)];
    [self UnderSingerTableViewBuild];
    
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
}

- (void)UnderSingerNetRequest {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger inTheSingerListNetRequest:self.singer_id withPage:self.pageChange] body:nil headerFile:nil response:XCJSON success:^(id result) {
        
        NSLog(@"%@", [XCUrlManger inTheSingerListNetRequest:self.singer_id withPage:self.pageChange]);
        
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"]) {
            XCUnderSingerDataModel *underSingerDataModel = [[XCUnderSingerDataModel alloc] initWithDataSource:dic];
            
            if (underSingerDataModel.singerName != nil && underSingerDataModel.urlList.count != 0 && [underSingerDataModel.urlList[0] objectForKey:@"url"] && underSingerDataModel.albumName != nil) {
                [self.dataSource addObject:underSingerDataModel];
                [underSingerDataModel release];
            }
            
        }
        XCUnderSingerDataModel *underSingerDataModel = self.dataSource[0];
        self.UnderSingerNameLable.text = underSingerDataModel.singerName;
        self.UnderSingerNameLable.backgroundColor = [UIColor yellowColor];
        self.page = result[@"pageCount"];
        
        [self.tableView reloadData];
        [self.tableView footerEndRefreshing];
        if (self.pageChange < self.page.integerValue) {
            self.pageChange += 1;
        }
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
}
- (void)UnderSingerTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.UnderSingerNameLable.frame = CGRectMake(0, high / 4 - 42, wide, 42);
    self.UnderSingerNameLable.font = [UIFont systemFontOfSize:25];
    
    self.UnderSingerHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 4 + 100)];
    self.UnderSingerHeadImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Image-%d", arc4random() % 10]];
    [self.UnderSingerHeadImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [view addSubview:self.UnderSingerNameLable];
    [view addSubview:self.UnderSingerHeadImageView];
    
    self.tableView.tableHeaderView = view;
    
    [self.tableView registerClass:[XCUnderSingerTableViewCell class] forCellReuseIdentifier:@"XCUnderSingerTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    
    [self.tableView footerBeginRefreshing];
    self.tableView.footerPullToRefreshText = @"上拉刷新数据";
    self.tableView.footerRefreshingText = @"正在刷新数据";
    self.tableView.footerReleaseToRefreshText = @"松开刷新数据";
    
    [view release];
    [_tableView release];
    [_UnderSingerHeadImageView release];
}
#pragma mark - 协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCUnderSingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCUnderSingerTableViewCell"];
    XCUnderSingerDataModel *underSingerDataModel = self.dataSource[indexPath.row];
    cell.underSingerModel = underSingerDataModel;
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.numberLable.font = [UIFont systemFontOfSize:25];
    

    if (underSingerDataModel.urlList == nil || underSingerDataModel.urlList.count == 0 || [underSingerDataModel.urlList isKindOfClass:[NSNull class]]) {
        
        cell.nameLable.textColor = [UIColor colorWithRed:1.000 green:0.521 blue:0.838 alpha:1.000];
        cell.albumNameLable.textColor = [UIColor colorWithRed:1.000 green:0.509 blue:0.747 alpha:1.000];
        
    } else {
        
        
        cell.nameLable.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
        cell.albumNameLable.textColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    }
    
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    XCUnderSingerDataModel *underSingerDataModel = self.dataSource[indexPath.row];
    
    if (underSingerDataModel.urlList.count != 0) {
        
            [[OnlineAVplay shareAVPlayer] playWithUrl:[underSingerDataModel.urlList[0] objectForKey:@"url"]];
            [[OnlineAVplay shareAVPlayer] play];
            [OnlineAVplay shareAVPlayer].isPlay = YES;
    }
        
  
    for (XCUnderSingerDataModel *underSingerDataModel in self.dataSource) {
        if (underSingerDataModel.urlList.count != 0 && underSingerDataModel.singerName != nil && [underSingerDataModel.urlList[0] objectForKey:@"url"] != nil) {

            if (underSingerDataModel.mvList.count != 0 && [underSingerDataModel.mvList[0] objectForKey:@"picUrl"] != nil) {
                [self.mutableHeadIVPicurl addObject:[underSingerDataModel.mvList[0] objectForKey:@"picUrl"]];
            } else {
                [self.mutableHeadIVPicurl addObject:@"http://b81.photo.store.qq.com/psb?/4f8f989b-6018-4a6b-b282-9fcf547c65b1/RN73JFjQje52jptUPtl5qMpL*M3jYfIuHnB0w5MlBPA!/b/YXG1SjAqdQAAYu.9SjASdAAA&bo=ngKuAQAAAAABABQ!&rf=viewer_4"];
            }
            [self.mutableArraySingerNAme addObject:underSingerDataModel.singerName];
            [self.nameArray addObject:underSingerDataModel.name];
            [self.mutableArraySongName addObject:[underSingerDataModel.urlList[0] objectForKey:@"url"]];
        }
    }

    
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.UnderSingerHeadImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.UnderSingerHeadImageView.transform = CGAffineTransformMakeScale(scale, scale);
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}

- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
